/**
 * web camera interface
 * @author Elliot
 * @date 2017-05-21
 */
(function (window, $) {
    var Camera = {
        init: function (options) {
            this.options = $.extend(this.defaultOptions, options || {});
            this.createContainer();
        },
        plugin: function () {
            return document.getElementById(this.id);
        },
        getWin: function () {
            return this.plugin().contentWindow;
        },
        createContainer: function () {
            var t = this;
            var options = t.options;
            t.id = options.id || 'camera_' + new Date().getTime();
            // t.options.device="resources/js/camera/device.html";
            var url = "resources/js/camera/device.html?type=" + options.type+"&facRect="+options.facRect+"&time="+new Date().getTime()+'&width='+options.viewWidth+'&height='+options.viewHeight;
            var ifr = '<iframe id="' + t.id + '" width="' + options.width + '" height="' + options.height + '" src="' + url + '" frameborder="0" scrolling="false" ></iframe>';
            $(options.container).html(ifr);

            // window.setTimeout(function () {
            //     t.getWin().location.reload();
            // }, 500);

        },
        destroy: function () {
            $(this.plugin()).remove();
        },
        scan: function (deviceType) {
            var win = this.getWin();
            return win.Scan(deviceType);
        },
        startIdCard:function(){
            var win = this.getWin();
            this.readerStatus=win.plugin().Global_InitIdCard();
            return this.readerStatus;
        },
        /**
         * 获取身份证信息，包含文字及图片（base64）
         * @returns {object}
         */
        readIdCardData: function () {
            return this.getWin().readIdCardData();
        },
        discernIdCard:function(){
            var win = this.getWin();
            this.readStatus=win.plugin().Global_ReadIdCard();
            return this.readStatus;
        },
        VerifyFaceDetect:function(){
           return this.getWin().VerifyFaceDetect();
        },
        readData:function(index){

            if(!this.readerStatus){
                this.readerStatus=this.startIdCard();
            }

            if(!this.discernIdCard()){
                this.readStatus=this.discernIdCard();
            }
            if(this.readerStatus!=1){
                alert("身份证读卡器未初始化成功");
                return;
            }
            if(this.readStatus!=1){
                alert("二代身份证未识别成功");
                return;
            }
            var win = this.getWin();
            return win.plugin().Global_GetIdCardData(index);
        },
        stopIdCard:function ()
        {
            var win = this.getWin();
            win.plugin().Global_ReaderStop();
            win.plugin().Global_DeinitReader();
        },

        defaultOptions: {
            id: '',
            container: document.body, // 容器
            type: 1, // 类型
            width: 500,
            height: 420,
            viewWidth:500,
            viewHeight:375,
            readerStatus:false,
            readStatus:false,
            device:1,
            deviceStatus:false,
            facRect:true
        }
    };

    window.Camera = Camera;

}(window, jQuery));